from django.http import HttpResponse
from django.shortcuts import render
from django.db.models import Avg,Count, Max, Min, Sum
from .models import Book,BookOrder,Publisher,Author

# Create your views here.
def avg_view(request):
    # result = Book.objects.aggregate(Avg('price'))
    result = Book.objects.aggregate(my_avg=Avg('price'))
    print(result)
    return HttpResponse("avg view")

def count_view(request):
    result = Book.objects.aggregate(my_count=Count('id'))
    print(result)
    return HttpResponse("count view")

def maxmin_view(request):
    result = Author.objects.aggregate(my_max=Max('age'), my_min=Min('age'))
    print(result)
    return HttpResponse("max_min view")

def sum_view(request):
    result = Book.objects.annotate(total=Sum("bookorder__price")).values('name', 'total')
    print(result)
    return HttpResponse("sum_view")